﻿
@{
    ViewBag.Title = "过站扣料配置";
    Layout = "~/Views/Shared/_LayoutPage.cshtml";
}
@using IOA.MES.DataAccess.Enum

<div id="page"></div>

<script type="text/template" id="tplPage">
    <div class="layui-fluid">
        <div class="layui-card">
            @foreach (var item in ViewBag.Sites as List<KeyValuePair<SiteType, string>>)
            {
                <div class="layui-card-body">
                    <input name="ikey" type="hidden" value="@item.Key" />
                    <h3 style="font-weight:bold">{{lang.Field.SiteName}}：@item.Value </h3>
                    <p style="margin:5px 0">
                        {{lang.Field.DeductionList}}:
                        <button class="layui-btn layui-btn-sm layuiadmin-btn-list" type="button" onclick="editBom('@(item.Key)');">
                            <i class="layui-icon layui-icon-edit layuiadmin-button-btn"></i>
                        </button>
                    </p>
                    <table id="table@(item.Key)" lay-filter="table"></table>
                </div>
            }
        </div>
    </div>
</script>

@section Scripts{
    <script>
        var vm = new Vue({
            el: "#page",
            template: "#tplPage",
            data: {
                lang: JSON.parse(lang).package
            }
        });
        var orderNo = "@ViewBag.OrderNo",
            workOrderNo = "@ViewBag.WorkOrderNo";

        setTimeout(function () {
            initLayui();
        }, 50);
        function initLayui() {
            layui.use(['table', 'layer'], function () {
                for (var ik = 0; ik < $("input[name='ikey']").length; ik++) {
                    var layer = layui.layer,
                        table = layui.table;

                    var tv = $("input[name='ikey']:eq(" + ik + ")").val();
                    var tid = 'table' + tv;

                    var options = {
                        elem: '#' + tid
                        , id: tid
                        , url: '/ParaSetting/QueryProductSiteBom'
                        , method: "post"
                        , where: { orderNo: orderNo, workOrderNo: workOrderNo, siteType: tv }
                        , page: {
                            limit: 15
                            , limits: [15, 20, 25, 30, 40, 50]
                        }
                        , response: {
                            statusName: 'ErrCode'
                            , statusCode: 200
                            , msgName: 'ErrCode'
                            , countName: 'TotalCount'
                            , dataName: 'Data'
                        }
                        , cols: [[
                            { title: '序号', width: 60, type:"numbers" }
                            , { field: 'BomCategoryCode', title: '分类编码', width: 100 }
                            , { field: 'BomCategoryName', title: '分类名称' }
                            , {
                                title: '操作', width: 200, templet: function (d) {
                                    var str = '<button class="layui-btn layui-btn-sm layui-btn-danger" onclick="deleteBOM(' + d.PkId + ',\'' + d.BomCategoryName + '\')"><i class="layui-icon">&#xe640;</i>删除</button>';
                                    return str;
                                }
                            }
                        ]]
                    };

                    table.render(options);
                }
            });
        }

        function editBom(siteType) {
            layer.open({
                type: 2,
                shade: [0.1, '#000'],
                area: ['500px', '600px'],
                content: "/ParaSetting/GlobalBomEdit?siteType=" + siteType + "&orderNo=" + orderNo + "&workOrderNo=" + workOrderNo,
                end: function () {
                    initLayui();
                }
            });
        }

        function deleteBOM(bomId, bomName) {
            layer.confirm('确定要删除“' + bomName + '”吗？', { icon: 3, title: '提示' }, function (index) {
                var loadId = layer.load();
                $.ajax({
                    url: "/ParaSetting/ProductSiteBomDelete?pkId=" + bomId,
                    complete: function () {
                        layer.close(loadId);
                    },
                    success: function (res) {
                        if (res.ErrCode != 200) {
                            layer.msg(res.ErrMsg, { icon: 5 });
                            return;
                        }
                        layer.msg("删除成功！", { icon: 6, time: 1500 });
                        initLayui();
                    }
                });
                layer.close(index);
            });
        }
    </script>
}
